<template>
    <div class="loginForm">
        <el-form :rules="rules" :model="loginData" status-icon ref="loginForm">
            <h3>系统登录</h3>
            <el-form-item prop="username" >
                <el-input prefix-icon="el-icon-user" placeholder="请输入用户名" size="small" type="text" v-model="loginData.username"></el-input>
            </el-form-item>
            <el-form-item prop="password" >
                <el-input  prefix-icon="el-icon-lock" placeholder="请输入密码" size="small" type="password" v-model="loginData.password"></el-input>
            </el-form-item>
            <el-form-item>
                <el-checkbox  v-model="rememberme">记住密码</el-checkbox>
            </el-form-item>
            <el-button class="loginbtn" size="small" type="primary" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    export default {
        name: "Login",

        data(){
            return{
                rules:{
                    username :[{required:true ,message:'请正确填写用户名称',trigger:'blur'}],
                    password:[{required: true,message: '请正确填写密码',trigger:'blur'}]
                },
                loginData:{
                    username:'admin',
                    password:'123'
                },
                rememberme:true,
            }
        },
        methods:{
            submitLogin(){
                this.$refs['loginForm'].validate((valid) => {
                    if (valid) {
                        //向后端发送请求
                        this.$postKeyValueRequest('/doLogin',this.loginData).then(resp=>{
                            if (resp){
                                window.sessionStorage.setItem('loginer',JSON.stringify(resp.data));
                                this.$router.replace('/home');
                                console.log(resp);
                            }
                        })
                    } else {
                        this.$message.warning ("请正确填写信息");
                        return false;
                    }
                });
            },


        }



    }
</script>

<style scoped>
    .loginForm{
        width: 400px;
        padding: 20px;
        margin: 180px auto;
    }
    .loginForm h3{
        text-align: center;
        color: #ffffff;
    }
    .loginForm .loginbtn{
        width: 100%;
    }

</style>